package com.loop;

import java.util.Random;
import java.util.Scanner;

/*
随机生成一个[1,100]之间的数据，提示用户猜测，猜大提示过大，猜小提示过小，直到猜中结束游戏。
*/
public class Demo08 {
    public static void main(String[] args) {
        Random random = new Random();
        int number = random.nextInt(100) + 1;
        Scanner sc = new Scanner(System.in);
        int counter = 0;
        int min = 1;
        int max = 100;
        while (true) {
            System.out.println("请输入你要猜的数字：");
            int searchnumber = sc.nextInt();
            counter++;
            if (searchnumber < number) {
                min = searchnumber + 1;
                System.out.println("你猜的数字过小，请在" + min + "~" + max + "之间重新输入：");
            } else if (searchnumber > number) {
                max = searchnumber - 1;
                System.out.println("你猜的数字过大，请在" + min + "~" + max + "之间重新输入：");
            } else {
                System.out.println("恭喜你猜对了");
                break;
            }
            if (counter == 5) {
                System.out.println(counter + "次机会用完了，结束游戏！");
                break;
            }
        }
    }
}
